//
// Created by 51399 on 2021/5/26.
//

#include "_thread.h"

void *print(void * v) {
    static char *res = "子线程响应内容";
    //运行取消
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
    //设置调用取消的时候立即退出
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
    while (1) {
        sleep(1);
        printf("子线程正在运行\n");
    }
    return (void *) res;
}
void _thread_create() {
    pthread_t t;
    if (pthread_create(&t, NULL, print, NULL) != 0) {
        perror("fail to create thread");
        exit(1);
    }
    sleep(3);
    /**
     * 无需等待join再释放资源,只要调用完毕自动释放
     */
    pthread_detach(t);
    int i = pthread_cancel(t);
    char *string;
    void **s = &string;
    pthread_join(t, s);
    char *pVoid = (char *)*s;
    printf("%s\n", pVoid);
#if 0
    if (pthread_detach(t) != 0) {
        perror("fail to create thread");
        exit(1);
    }
#endif
}

